package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

import com.qy.dao.DriverInfoDao;

import java.util.List;
import java.util.Map;
import java.sql.Types;

import com.qy.db.DbParameter;

import java.util.HashMap;
import java.util.ArrayList;

import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.DriverInfo;
import com.qy.db.BaseDao;

/**
* u_driver_info daoImpl实现类的生成
* @author zhangwenlong
* @date Sat Feb 10 17:49:10 GMT+08:00 2018
*/ 
public class DriverInfoDaoImpl extends BaseDao  implements DriverInfoDao {

	@Override
	public boolean createDriverInfo(DriverInfo driverInfo) {
		boolean result = false;
		int id = -1;
		if (driverInfo.beginAdd()) {
			String sql = "insert into u_driver_info (id,third_id,image,login_date,login_ip,user_type,user_name,sex,user_status,create_date,phone,driving_licence_image1,driving_licence_image2,card,money,tmp_secret,bank_name,bank_car) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, driverInfo.getId()));
			param.put(2, new DbParameter(Types.VARCHAR, driverInfo.getThirdId()));
			param.put(3, new DbParameter(Types.VARCHAR, driverInfo.getImage()));
			param.put(4, new DbParameter(Types.TIMESTAMP, driverInfo.getLoginDate()));
			param.put(5, new DbParameter(Types.VARCHAR, driverInfo.getLoginIp()));
			param.put(6, new DbParameter(Types.INTEGER, driverInfo.getUserType()));
			param.put(7, new DbParameter(Types.VARCHAR, driverInfo.getUserName()));
			param.put(8, new DbParameter(Types.INTEGER, driverInfo.getSex()));
			param.put(9, new DbParameter(Types.INTEGER, driverInfo.getUserStatus()));
			param.put(10, new DbParameter(Types.TIMESTAMP, driverInfo.getCreateDate()));
			param.put(11, new DbParameter(Types.VARCHAR, driverInfo.getPhone()));
			param.put(12, new DbParameter(Types.VARCHAR, driverInfo.getDrivingLicenceImage1()));
			param.put(13, new DbParameter(Types.VARCHAR, driverInfo.getDrivingLicenceImage2()));
			param.put(14, new DbParameter(Types.VARCHAR, driverInfo.getCard()));
			param.put(15, new DbParameter(Types.INTEGER, driverInfo.getMoney()));
			param.put(16, new DbParameter(Types.VARCHAR, driverInfo.getTmpSecret()));
			param.put(17, new DbParameter(Types.VARCHAR, driverInfo.getBankName()));
			param.put(18, new DbParameter(Types.VARCHAR, driverInfo.getBankCar()));
			id = execLastId(sql, param);
			result = id > -1;
			driverInfo.commitAdd(result);
			driverInfo.setId(id);
		}
		return result;
	}

	@Override
	public List<DriverInfo> getDriverInfoList() {
		String sql = "select * from u_driver_info;";
		PreparedStatement pstmt = execQuery(sql, null);
		ResultSet rs = null;
		List<DriverInfo> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<DriverInfo>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public DriverInfo getDriverInfoById(int id) {
		String sql = "select * from u_driver_info where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		DriverInfo driverInfo = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					driverInfo = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return driverInfo;
	}

	@Override
	public boolean updateDriverInfo(DriverInfo driverInfo) {
		boolean result = false;
		if (driverInfo.beginUpdate()) {
			String sql = "update u_driver_info set third_id=?,image=?,login_date=?,login_ip=?,user_type=?,user_name=?,sex=?,user_status=?,create_date=?,phone=?,driving_licence_image1=?,driving_licence_image2=?,card=?,money=?,tmp_secret=?,bank_name=?,bank_car=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.VARCHAR, driverInfo.getThirdId()));
			param.put(2, new DbParameter(Types.VARCHAR, driverInfo.getImage()));
			param.put(3, new DbParameter(Types.TIMESTAMP, driverInfo.getLoginDate()));
			param.put(4, new DbParameter(Types.VARCHAR, driverInfo.getLoginIp()));
			param.put(5, new DbParameter(Types.INTEGER, driverInfo.getUserType()));
			param.put(6, new DbParameter(Types.VARCHAR, driverInfo.getUserName()));
			param.put(7, new DbParameter(Types.INTEGER, driverInfo.getSex()));
			param.put(8, new DbParameter(Types.INTEGER, driverInfo.getUserStatus()));
			param.put(9, new DbParameter(Types.TIMESTAMP, driverInfo.getCreateDate()));
			param.put(10, new DbParameter(Types.VARCHAR, driverInfo.getPhone()));
			param.put(11, new DbParameter(Types.VARCHAR, driverInfo.getDrivingLicenceImage1()));
			param.put(12, new DbParameter(Types.VARCHAR, driverInfo.getDrivingLicenceImage2()));
			param.put(13, new DbParameter(Types.VARCHAR, driverInfo.getCard()));
			param.put(14, new DbParameter(Types.INTEGER, driverInfo.getMoney()));
			param.put(15, new DbParameter(Types.VARCHAR, driverInfo.getTmpSecret()));
			param.put(16, new DbParameter(Types.VARCHAR, driverInfo.getBankName()));
			param.put(17, new DbParameter(Types.VARCHAR, driverInfo.getBankCar()));
			param.put(18, new DbParameter(Types.INTEGER, driverInfo.getId()));
			result = execNoneQuery(sql, param) > -1;
			driverInfo.commitUpdate(result);
		}
		return result;
	}

	public DriverInfo resultTo(ResultSet rs) throws SQLException {
		DriverInfo driverInfo = new DriverInfo();
		driverInfo.setId(rs.getInt("id"));
		driverInfo.setThirdId(rs.getString("third_id"));
		driverInfo.setImage(rs.getString("image"));
		driverInfo.setLoginDate(rs.getTimestamp("login_date"));
		driverInfo.setLoginIp(rs.getString("login_ip"));
		driverInfo.setUserType(rs.getInt("user_type"));
		driverInfo.setUserName(rs.getString("user_name"));
		driverInfo.setSex(rs.getInt("sex"));
		driverInfo.setUserStatus(rs.getInt("user_status"));
		driverInfo.setCreateDate(rs.getTimestamp("create_date"));
		driverInfo.setPhone(rs.getString("phone"));
		driverInfo.setDrivingLicenceImage1(rs.getString("driving_licence_image1"));
		driverInfo.setDrivingLicenceImage2(rs.getString("driving_licence_image2"));
		driverInfo.setCard(rs.getString("card"));
		driverInfo.setMoney(rs.getInt("money"));
		driverInfo.setTmpSecret(rs.getString("tmp_secret"));
		driverInfo.setBankName(rs.getString("bank_name"));
		driverInfo.setBankCar(rs.getString("bank_car"));
		driverInfo.setOp(Option.None);
		return driverInfo;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}

	@Override
	public DriverInfo getDriverInfoByOpenid(String openid) {
		String sql = "select * from u_driver_info where third_id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.VARCHAR, openid));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		DriverInfo driverInfo = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					driverInfo = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return driverInfo;
	}
}

